/**
 *  ward @ 2019
 *  harrydeep@wardyan.top
 */
package top.wardyan.wdev.dbdevassist.action.base;

import java.util.List;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.ScrolledComposite;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.TableItem;

import top.wardyan.conf.config.SQLite;
import top.wardyan.control.WButton;
import top.wardyan.control.WComposite;
import top.wardyan.control.WTable;
import top.wardyan.util.Msg;
import top.wardyan.util.ShellUtil;
import top.wardyan.wdev.dbdevassist.config.DDA;
import top.wardyan.wdev.dbdevassist.model.DetailModel;
import top.wardyan.wdev.dbdevassist.model.MasterModel;
import top.wardyan.wdev.dbdevassist.service.IDetailService;
import top.wardyan.wdev.dbdevassist.service.IMasterService;

/**
 * 只有一个明细表的
 * @author ward 2020-05-16
 */
public abstract class BaseMasterDetailEditShell<T extends BaseMasterDetailEditShell<T, M, D>, 
												M extends MasterModel<M>, 
												D extends DetailModel<D>> 
										extends BaseMasterEditShell<T, M>{

	protected WComposite mainCom;
	protected IDetailService<D> detailService;
	protected WComposite detailCom;
	protected WButton btnDetailAdd;
	//WButton btnDetailSave;
	protected WButton btnDetailDel;
	protected WButton btnDetailClear;
	protected WButton btnDetailRefresh;
	//获取
	protected WComposite detailTopCom;
	
	protected ScrolledComposite comDetailTable;
	protected WTable tblDetailTable;
	
	int pad = 14;
	
	public BaseMasterDetailEditShell(int sID, IMasterService<M> masterService, IDetailService<D> detailService) {
		super(sID, masterService);
		this.detailService = detailService;
		
		this.createContents();
	}
	

	private void createContents() {
		// detail
		mainCom = new WComposite(this, SWT.BORDER);
		mainCom.setLayout(new FormLayout());
		//mainCom.setLayout(layout);
		FormData fd_mainCom = new FormData();
		fd_mainCom.left = new FormAttachment(0);
		fd_mainCom.top = new FormAttachment(topCom, 5);
		fd_mainCom.right = new FormAttachment(100, -1);
		//fd_mainCom.bottom = new FormAttachment(mainCom, 40);
		mainCom.setLayoutData(fd_mainCom);
		
		detailCom = new WComposite(this, SWT.BORDER);
		detailCom.setLayout(new FormLayout());
		FormData fd_detailCom = new FormData();
		fd_detailCom.left = new FormAttachment(0);
		fd_detailCom.top = new FormAttachment(mainCom, 5);
		fd_detailCom.right = new FormAttachment(100, -1);
		fd_detailCom.bottom = new FormAttachment(100, -1);
		detailCom.setLayoutData(fd_detailCom);
		
		
		
		detailTopCom = new WComposite(detailCom, SWT.BORDER);
		detailTopCom.setLayout(new FormLayout());
		FormData fd_detailTopCom = new FormData();
		fd_detailTopCom.left = new FormAttachment(0);
		fd_detailTopCom.top = new FormAttachment(detailCom, 2);
		fd_detailTopCom.right = new FormAttachment(100, -1);
		fd_detailTopCom.bottom = new FormAttachment(detailTopCom, 40);
		detailTopCom.setLayoutData(fd_detailTopCom);
		
		btnDetailAdd = new WButton(detailTopCom, SWT.PUSH);
		btnDetailAdd.setText("新增");
		FormData fd_btnDetailAdd = new FormData();
		fd_btnDetailAdd.left = new FormAttachment(detailTopCom, 10);
		fd_btnDetailAdd.top = new FormAttachment(detailTopCom, 2);
		fd_btnDetailAdd.right = new FormAttachment(btnDetailAdd, pad * 4);
		fd_btnDetailAdd.bottom = new FormAttachment(btnDetailAdd, 30);
		btnDetailAdd.setLayoutData(fd_btnDetailAdd);
		btnDetailAdd.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				//new DicItemEditShell(self.sID, 0, dda.dicItemService).go();
				self.getEditShell(0).go();
				self.refreshDetails();
			}
			
		});
		
		btnDetailDel = new WButton(detailTopCom, SWT.PUSH);
		btnDetailDel.setText("删除");
		FormData fd_btnDetailDel = new FormData();
		fd_btnDetailDel.left = new FormAttachment(btnDetailAdd, 10);
		fd_btnDetailDel.top = new FormAttachment(detailTopCom, 2);
		fd_btnDetailDel.right = new FormAttachment(btnDetailDel, pad * 4);
		fd_btnDetailDel.bottom = new FormAttachment(btnDetailDel, 30);
		btnDetailDel.setLayoutData(fd_btnDetailDel);
		btnDetailDel.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				int index = self.tblDetailTable.getSelectionIndex();
				if(index >= 0) {
					TableItem item = self.tblDetailTable.getItem(index);
					top.wardyan.conf.model.YTTableCol tc = SQLite.getInstance(DDA.SQLTEMPLATE).tablecolService.find(self.getClass().getName(), "tblDetailTable", "dID");
					if(tc != null) {
						int dID = Integer.valueOf(item.getText(tc.getSIndex()));
						if(Msg.confirm(self, "警告", "确定删除吗?注意,删除操作不可恢复", SWT.OK | SWT.CANCEL) == SWT.OK) {
							detailService.deleteByIDs(self.sID, dID);
							self.refreshDetails();
						}
						
					}
				}
			}
			
		});
		
		btnDetailClear = new WButton(detailTopCom, SWT.PUSH);
		btnDetailClear.setText("清空");
		FormData fd_btnDetailClear = new FormData();
		fd_btnDetailClear.left = new FormAttachment(btnDetailDel, 10);
		fd_btnDetailClear.top = new FormAttachment(detailTopCom, 2);
		fd_btnDetailClear.right = new FormAttachment(btnDetailClear, pad * 4);
		fd_btnDetailClear.bottom = new FormAttachment(btnDetailClear, 30);
		btnDetailClear.setLayoutData(fd_btnDetailClear);
		
		btnDetailClear.addSelectionListener(new SelectionAdapter() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				if(self.tblDetailTable.getItems().length > 0 && Msg.confirm(self, "警告", "确定清空吗?注意,清空操作不可恢复", SWT.OK | SWT.CANCEL) == SWT.OK) {
					List<D> dis = detailService.list(self.sID);
					if(dis != null) {
						for(D d : dis)
							detailService.delete(d);
						self.refreshDetails();
					}
				}
			}
			
		});

		btnDetailRefresh = new WButton(detailTopCom, SWT.PUSH);
		btnDetailRefresh.setText("刷新");
		FormData fd_btnDetailRefresh = new FormData();
		fd_btnDetailRefresh.left = new FormAttachment(btnDetailClear, 10);
		fd_btnDetailRefresh.top = new FormAttachment(detailTopCom, 2);
		fd_btnDetailRefresh.right = new FormAttachment(btnDetailRefresh, pad * 4);
		fd_btnDetailRefresh.bottom = new FormAttachment(btnDetailRefresh, 30);
		btnDetailRefresh.setLayoutData(fd_btnDetailRefresh);
		btnDetailRefresh.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				// self.tblDetailTable.show("tblDetailTable", "", self.detailService.list(self.sID));
				self.refreshDetails();
			}
		});
		
		
		
		
		//
		//comDetailTable = new WComposite(detailCom, SWT.BORDER);
		comDetailTable = new ScrolledComposite(detailCom, SWT.BORDER);
		comDetailTable.setLayout(new FormLayout());
		FormData fd_comDetailTable = new FormData();
		fd_comDetailTable.left = new FormAttachment(0);
		fd_comDetailTable.top = new FormAttachment(detailTopCom, 2);
		fd_comDetailTable.right = new FormAttachment(100, -1);
		fd_comDetailTable.bottom = new FormAttachment(comDetailTable, 300);
		comDetailTable.setLayoutData(fd_comDetailTable);
		
		tblDetailTable = new WTable(comDetailTable, SWT.NONE, "tblDetailTable", self.detailService);
		FormData fd_tblDetailTable = new FormData();
		fd_tblDetailTable.left = new FormAttachment(0);
		fd_tblDetailTable.top = new FormAttachment(comDetailTable, 2);
		//fd_tblDetailTable.right = new FormAttachment(100, -10);
		//fd_tblDetailTable.bottom = new FormAttachment(comDetailTable, 300);
		fd_tblDetailTable.bottom = new FormAttachment(100, -5);
		tblDetailTable.setLayoutData(fd_tblDetailTable);
		//tblDetailTable.setSize(100, 100);
		//WTableColumn c = new WTableColumn(tblDetailTable, SWT.NONE);
		//tblDetailTable.setDatas(detailService.list(this.sID));
		//tblDetailTable.show("tblDetailTable", "");
		self.refreshDetails();
		//tblDetailTable.setService(self.detailService);
		
		tblDetailTable.addMouseListener(new MouseAdapter() {

			@Override
			public void mouseDoubleClick(MouseEvent e) {
				if(self.model != null && self.model.getSStatus().getSID() == 2) {
					int index = tblDetailTable.getSelectionIndex();
					if(index >= 0) {
						TableItem item = self.tblDetailTable.getItem(index);
						top.wardyan.conf.model.YTTableCol tc = SQLite.getInstance(DDA.SQLTEMPLATE).tablecolService.find(self.getClass().getName(), "tblDetailTable", "dID");
						if(tc != null) {
							int dID = Integer.valueOf(item.getText(tc.getSIndex()));
							//new DicItemEditShell(self.sID, dID, dda.dicItemService).go();
							self.getEditShell(dID).go();
							self.refreshDetails();
						}
					}
				}
			}
			
		});
		
		ShellUtil.center(self);
	
	}
	
	protected abstract BaseDetailEditShell<? extends BaseDetailEditShell<?, D>, D> getEditShell(int dID);
	
	
	
	/**
	 * 重置
	 */
	//protected abstract void reset();
	/**
	 * 清空
	 */
	protected void clear() {
		this.show();
		self.initBtnStatus(false);
	}
	
	protected abstract void refreshDetails();
}
